package main

import (
	"github.com/boltdb/bolt"
	"whoscored/util/boltdb"
	"fmt"
	"encoding/json"
	"strconv"
	"strings"

	"github.com/gocolly/colly"
	"github.com/PuerkitoBio/goquery"
	"time"
	"math/rand"
)
var bingo=0
func preview(match []interface{},i int) {

	matchId:=strconv.FormatFloat(match[1].(float64),'f',-1,64)
	s:=strings.Replace(match[12].(string)," ","",-1)
	ss:=strings.Split(s,":")

	home,_:=strconv.ParseFloat(ss[0],64)
	away,_:=strconv.ParseFloat(ss[1],64)
	url:="https://www.whoscored.com/Matches/"+matchId+"/Preview/"
	c := colly.NewCollector()
	c.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
	c.OnRequest(func(r *colly.Request) {

		r.Headers.Set("Pragma", "no-cache")
		r.Headers.Set("authority", "www.whoscored.com")
		r.Headers.Set("method", "GET")
		r.Headers.Set("Cookie", "visid_incap_774904=iINxyvH1RSKdJVocn4S/cgCjZVoAAAAAQUIPAAAAAABSHTN36SBh6uyX/MAX4UcQ; _ga=GA1.2.586383088.1516610314; permutive-id=ab06f937-443e-4432-83d7-ab4e2b556060; __gads=ID=6da13b6a195043d9:T=1516610316:S=ALNI_MZbNFloOsGT8E_4RTHnzKBgU0cuig; vg=821ae0b8-2b61-46af-a8b4-21331b25c154; ip=3063167153; visid_incap_774908=6hgTWJYURbKvDwiPKdeFZgzDbloAAAAAQUIPAAAAAAAya8xmGAjnhg8S+l3NdgjJ; vl=1:-8.00|2:CN|3:SICHUAN|4:|5:SICHUAN/|6:SICHUAN/CHENGDU|7:610015|!0; vd=chinatelecom.com.cn; incap_ses_432_774904=xHrJQHuFX2pD9pUf0sb+BczUpVoAAAAAqut5nb0KssyugulzSAC4IQ==; _gid=GA1.2.2073299497.1520817357; OX_plg=pm; fq=1; _psegs=%5B1920%2C1930%2C2126%2C2441%2C4848%2C3376%2C1907%2C1932%2C4139%2C1956%2C4167%2C2300%5D; _dm_sync=true; vq=4919,4919,4919,4919,4425,4919,4919,4919,4919,4919,4919,4919,4919,4425,4919,4919,4425,4425,4425,4425,4425,4425,4425,4425,4425,4425,4425,4425; _gat=1; _gat_subdomainTracker=1; _gat_streamamp=1; permutive-session=%7B%22session_id%22%3A%22ea8192b7-1087-4eb3-90a7-64a51262f6dd%22%2C%22last_updated%22%3A%222018-03-12T02%3A33%3A52.694Z%22%7D")
		r.Headers.Set("Connection", "keep-alive")
		r.Headers.Set("Accept", "application/json, text/javascript, */*; q=0.01")
		r.Headers.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,lb;q=0.7")
		r.Headers.Set("X-Requested-With", "XMLHttpRequest")
	})
	c.OnHTML("#preview-prediction", func(e *colly.HTMLElement) {
		h:=float64(0)
		a:=float64(0)
		e.DOM.Find(".predicted-score").Each(func(i int, s *goquery.Selection){
			if i==0{
				h,_=strconv.ParseFloat(s.Text(),64)
			}
			if i==1{
				a,_=strconv.ParseFloat(s.Text(),64)
			}
		})
		if (home>away&&h>a)||(home==away&&h==a)||(home<away&&h<a){
			bingo++
		}
		fmt.Println(ss,h,a)
		fmt.Println(bingo,i)
		seed:=1000+rand.Intn(5000)
		time.Sleep(time.Millisecond*time.Duration(seed))
	})

	// Before making a request print "Visiting ..."
	c.OnRequest(func(r *colly.Request) {
	})
	c.OnError(func(r *colly.Response, err error) {
		fmt.Println("Error Request URL:", r.Request.URL)
	})

	c.Visit(url)
}
func main()  {
	i:=0

	boltdb.ScanTx("matchs-2017", func(tx *bolt.Tx) {

		tx.ForEach(func(name []byte, b *bolt.Bucket) error {

			b.ForEach(func(k, v []byte) error {

				mmJson:=make(map[string]interface{},0)

				json.Unmarshal(v,&mmJson)
				region:=mmJson["region"].([]interface{})
				match:=mmJson["match"].([]interface{})

				league:=region[3]
				t:=region[len(region)-1]
				if league=="Netherlands"&&t=="Eredivisie"{
					i++
					preview(match,i)

				}

				return nil
			})
			return nil
		})
	})
	fmt.Println(i)
}
